home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / TCL1 / FANGS__ / OFFSETNU.C < prev    next >
Text File  |  1990-05-26  |  4KB  |  163 lines

  1. /* ⌐ 1990 Patrick Doane. */
  2.  
  3. #include <OffsetNum.h>
  4. #include <Utilities.h>
  5.  
  6. extern GrafPtr    scratchPort;
  7.  
  8. void    OffsetNum::IOffsetNum(WindowPtr theWindow,Rect *theBounds,
  9.     short theCurrentValue,short theMinValue,short theMaxValue,
  10.     short theStepValue,VoidFunc theChangeProc,long theDelay)
  11. {
  12.     Graphic::IGraphic(theWindow,theBounds);
  13.     clickOK = true;
  14.     
  15.     arrowHandle = (SICNHand)GetResource('SICN',128);
  16.     SetRect(&arrowBounds,bounds.left+18,bounds.top,bounds.left+34,
  17.         bounds.top+16);
  18.         
  19.     currentValue = theCurrentValue;
  20.     offset = 0;
  21.     minValue = theMinValue;
  22.     maxValue = theMaxValue;
  23.     stepValue = theStepValue;
  24.     if (theChangeProc != 0L)
  25.         changeProc = theChangeProc;
  26.     else
  27.         changeProc = DefaultProc;
  28.     timeDelay = theDelay;
  29. }
  30.  
  31. void    OffsetNum::SetBounds(Rect *theBounds)
  32. {
  33.     bounds = *theBounds;
  34.     SetRect(&arrowBounds,bounds.left+18,bounds.top,bounds.left+34,
  35.         bounds.top+16);
  36. }
  37.  
  38. void    OffsetNum::SetValue(short theValue,Boolean addOffset,Boolean redraw)
  39. {
  40.     if (addOffset)
  41.         currentValue = theValue + offset;
  42.     else
  43.         currentValue = theValue;
  44.     
  45.     if (currentValue < minValue)
  46.         currentValue = minValue;
  47.     if (currentValue > maxValue)
  48.         currentValue = maxValue;
  49.     
  50.     if (redraw)
  51.         UpdateText();
  52. }
  53.  
  54. void    OffsetNum::Update(void)
  55. {
  56.     Rect    theBounds;
  57.     
  58.     UpdateText();
  59.     theBounds = arrowBounds;
  60.     PlotSICN(&theBounds,arrowHandle,0);
  61. }
  62.  
  63. void    OffsetNum::UpdateText(void)
  64. {
  65.     Rect    theBounds;
  66.     Str255    theString;
  67.     GrafPtr    savePort;
  68.     
  69.     if (currentValue == 0x8000)
  70.         return;
  71.     GetPort(&savePort);
  72.     SetPort(scratchPort);
  73.     theBounds = bounds;
  74.     theBounds.right -= 16;
  75.     EraseRect(&theBounds);
  76.     MoveTo(theBounds.left,theBounds.top+12);
  77.     NumToString((long)currentValue,theString);
  78.     TextFont(savePort->txFont);
  79.     DrawString(theString);
  80.     SetPort(savePort);
  81.     CopyBits(&scratchPort->portBits,&thePort->portBits,&theBounds,
  82.         &theBounds,srcCopy,0L);
  83. }
  84.  
  85. void    OffsetNum::DoClick(EventRecord *theEvent)
  86. {
  87.     Rect    theRect;
  88.     Str255    theString;
  89.     Boolean    oldState = true,newState;
  90.     Point    mouseLoc;
  91.     
  92.     if (!active)
  93.         return;
  94.     theRect = arrowBounds;
  95.     theRect.bottom -= 8;
  96.     theRect.right -= 7;
  97.     if (PtInRect(theEvent->where,&theRect))
  98.     {    InvertRect(&theRect);
  99.         Increase();
  100.         while (TrackMouse(&mouseLoc,&theRect,&newState,timeDelay))
  101.         {    if (newState != oldState)
  102.             {    InvertRect(&theRect);
  103.                 oldState = newState;
  104.             }
  105.             if (oldState)
  106.                 Increase();
  107.         }
  108.         if (oldState)
  109.             InvertRect(&theRect);
  110.     }
  111.     theRect = arrowBounds;
  112.     theRect.top += 8;
  113.     theRect.right -= 7;
  114.     if (PtInRect(theEvent->where,&theRect))
  115.     {    InvertRect(&theRect);
  116.         Decrease();
  117.         while (TrackMouse(&mouseLoc,&theRect,&newState,timeDelay))
  118.         {    if (newState != oldState)
  119.             {    InvertRect(&theRect);
  120.                 oldState = newState;
  121.             }
  122.             if (oldState)
  123.                 Decrease();
  124.         }
  125.         if (oldState)
  126.             InvertRect(&theRect);
  127.     }
  128. }
  129.  
  130. void    OffsetNum::Increase(void)
  131. {
  132.     (*changeProc)(this,DoIncrease);
  133. }
  134.  
  135. void    OffsetNum::Decrease(void)
  136. {
  137.     (*changeProc)(this,DoDecrease);
  138. }
  139.  
  140. void    DefaultProc(register OffsetNum *theOffsetNum,short theParam)
  141. {
  142.     switch (theParam)
  143.     {    case DoIncrease:
  144.             if (theOffsetNum->currentValue + theOffsetNum->stepValue >
  145.                     theOffsetNum->maxValue)
  146.                 return;
  147.             
  148.             theOffsetNum->currentValue += theOffsetNum->stepValue;
  149.             theOffsetNum->offset += theOffsetNum->stepValue;
  150.             theOffsetNum->UpdateText();
  151.             break;
  152.         case DoDecrease:
  153.             if (theOffsetNum->currentValue - theOffsetNum->stepValue <
  154.                     theOffsetNum->minValue)
  155.                 return;
  156.             
  157.             theOffsetNum->currentValue += theOffsetNum->stepValue;
  158.             theOffsetNum->offset += theOffsetNum->stepValue;
  159.             
  160.             theOffsetNum->UpdateText();
  161.             break;
  162.     }
  163. }